package com.test.daily.leetcode.y2022.m05.day0503.v1;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author Tom on 2022/5/3
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println("m w".compareTo("mo"));
        String[] logs = new String[]{"j mo", "5 m w"};
        String[] orderedLogs = new Solution().reorderLogFiles(logs);
        for (int i = 0; i < orderedLogs.length; i++) {
            System.out.print(" " + orderedLogs[i]);
        }
    }
    public String[] reorderLogFiles(String[] logs) {
        String[] reOrderStrings = new String[logs.length];
        PriorityQueue<KV> pq = new PriorityQueue<>(new MyComparator());
        for (int i = 0; i < logs.length; i++) {
            KV kV = new KV(logs[i], i);
            pq.add(kV);
        }
        int i = 0;
        while(!pq.isEmpty()){
            reOrderStrings[i] = pq.poll().content;
            ++i;
        }
        return reOrderStrings;
    }
    public class MyComparator implements Comparator<KV>{
        @Override
        public int compare(KV o1, KV o2) {
            int typeO1 = 0;
            int typeO2 = 0;
            int o1ContentBeginIndex = o1.content.split(" ",2)[0].length() + 2;
            int o2ContentBeginIndex = o2.content.split(" ",2)[0].length() + 2;
            char o1Char = o1.content.charAt(o1ContentBeginIndex);
            char o2Char = o2.content.charAt(o2ContentBeginIndex);
            if( o1Char>='0' && o1Char<='9'){
                typeO1 = 0;
            } else {
                typeO1 = 1;
            }
            if( o2Char>='0' && o2Char<='9'){
                typeO2 = 0;
            } else {
                typeO2 = 1;
            }

            if(typeO1 ==0 && typeO2 ==0){
                return o1.index - o2.index;
            }
            if(typeO1 ==0 && typeO2 ==1){
                return 1;
            }
            if(typeO1 ==1 && typeO2 ==0){
                return -1;
            }
            if(!o1.content.substring(o1ContentBeginIndex, o1.content.length() -1).equals(o2.content.substring(o2ContentBeginIndex, o2.content.length() -1))){
               return  o1.content.substring(o1ContentBeginIndex, o1.content.length() -1).compareTo(o2.content.substring(o2ContentBeginIndex, o2.content.length() -1));
            } else {
                String t1 = o1.content.substring(0, o1ContentBeginIndex);
                String t2 = o2.content.substring(0, o2ContentBeginIndex);

                return t1.compareTo(t2);
            }
        }
    }
    public class KV{
        public String content;
        public int index;
        public KV(String content, int index){
            this.content = content;
            this.index = index;
        }
    }
}
